package com.xfusion.chatglm.data.config;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWords;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 选用了github的houbb的sensitive
 * https://github.com/houbb/sensitive
 * 敏感词配置
 */
@Slf4j
@Configuration
public class SensitiveWordConfig {

    @Bean
    public SensitiveWordBs sensitiveWords(){
        return SensitiveWordBs.newInstance()
                .wordReplace((stringBuilder, chars, iWordResult, iWordContext) -> {
                    String sensitiveWord = InnerWordCharUtils.getString(chars,iWordResult);
                    //使用InnerWordCharUtils.getString(chars,iWordResult)获取敏感词
                    log.info("敏感词：{}",sensitiveWord);
                    //日志输出

                    //敏感词替换
                    /*
                    替换操作，可以替换为*或者其他的词
                    else{
                        int wordLength = iWordResult.endIndex() - iWordResult.startIndex();
                        for(int i  = 0;i<wordLength;i++){
                            stringBuilder.append("");
                        }
                    }
                     */
                })
                .ignoreCase(true)//忽略大小写
                .ignoreWidth(true)//忽略全角半角的差异
                .ignoreNumStyle(true)//忽略数字样式的差异
                .ignoreChineseStyle(true)//忽略中文样式的差异
                .ignoreEnglishStyle(true)//忽略英文样式的差异
                .ignoreRepeat(false)//忽略重复的敏感词
                .enableNumCheck(true)//是否开启数字检查
                .enableEmailCheck(true)//是否开启邮箱检查
                .enableUrlCheck(true)//是否开启url检查
                .enableWordCheck(true)//是否开启敏感词检查
                .numCheckLen(1024)//数字检查长度
                .init();


    }
}
